home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 13 / CU Amiga Magazine's Super CD-ROM 13 (1997)(EMAP Images)(GB)(Track 1 of 2)[!][issue 1997-08].iso / CUCD / Graphics / Ghostscript / src / libpng / pngrutil.c < prev    next >
C/C++ Source or Header  |  1996-06-07  |  39KB  |  1,410 lines

  1.  
  2. /* pngrutil.c - utilities to read a png file
  3.  
  4.    libpng 1.0 beta 3 - version 0.89
  5.    For conditions of distribution and use, see copyright notice in png.h
  6.    Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.
  7.    May 25, 1996
  8.    */
  9.  
  10. #define PNG_INTERNAL
  11. #include "png.h"
  12.  
  13. /* grab an uint 32 from a buffer */
  14. png_uint_32
  15. png_get_uint_32(png_bytep buf)
  16. {
  17.    png_uint_32 i;
  18.  
  19.    i = ((png_uint_32)(*buf) << 24) +
  20.       ((png_uint_32)(*(buf + 1)) << 16) +
  21.       ((png_uint_32)(*(buf + 2)) << 8) +
  22.       (png_uint_32)(*(buf + 3));
  23.  
  24.    return i;
  25. }
  26.  
  27. /* grab an uint 16 from a buffer */
  28. png_uint_16
  29. png_get_uint_16(png_bytep buf)
  30. {
  31.    png_uint_16 i;
  32.  
  33.    i = (png_uint_16)(((png_uint_16)(*buf) << 8) +
  34.       (png_uint_16)(*(buf + 1)));
  35.  
  36.    return i;
  37. }
  38.  
  39. /* read data, and run it through the crc */
  40. void
  41. png_crc_read(png_structp png_ptr, png_bytep buf, png_uint_32 length)
  42. {
  43.    png_read_data(png_ptr, buf, length);
  44.    png_calculate_crc(png_ptr, buf, length);
  45. }
  46.  
  47. /* skip data, but calcuate the crc anyway */
  48. void
  49. png_crc_skip(png_structp png_ptr, png_uint_32 length)
  50. {
  51.    png_uint_32 i;
  52.  
  53.    for (i = length; i > png_ptr->zbuf_size; i -= png_ptr->zbuf_size)
  54.    {
  55.       png_read_data(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size);
  56.       png_calculate_crc(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size);
  57.    }
  58.    if (i)
  59.    {
  60.       png_read_data(png_ptr, png_ptr->zbuf, i);
  61.       png_calculate_crc(png_ptr, png_ptr->zbuf, i);
  62.    }
  63. }
  64.  
  65. /* read and check the IDHR chunk */
  66. void
  67. png_handle_IHDR(png_structp png_ptr, png_infop info, png_uint_32 length)
  68. {
  69.    png_byte buf[13];
  70.    png_uint_32 width, height;
  71.    int bit_depth, color_type, compression_type, filter_type;
  72.    int interlace_type;
  73.  
  74.    if (png_ptr->mode != PNG_BEFORE_IHDR)
  75.       png_error(png_ptr, "Out of place IHDR");
  76.  
  77.    /* check the length */
  78.    if (length != 13)
  79.       png_error(png_ptr, "Invalid IHDR chunk");
  80.  
  81.    png_crc_read(png_ptr, buf, 13);
  82.  
  83.    width = png_get_uint_32(buf);
  84.    height = png_get_uint_32(buf + 4);
  85.    bit_depth = buf[8];
  86.    color_type = buf[9];
  87.    compression_type = buf[10];
  88.    filter_type = buf[11];
  89.    interlace_type = buf[12];
  90.  
  91.    /* check for width and height valid values */
  92.    if (width == 0 || height == 0)
  93.       png_error(png_ptr, "Invalid image size in IHDR");
  94.  
  95.    /* check other values */
  96.    if (bit_depth != 1 && bit_depth != 2 &&
  97.       bit_depth != 4 && bit_depth != 8 &&
  98.       bit_depth != 16)
  99.       png_error(png_ptr, "Invalid bit depth in IHDR");
  100.  
  101.    if (color_type < 0 || color_type == 1 ||
  102.       color_type == 5 || color_type > 6)
  103.       png_error(png_ptr, "Invalid color type in IHDR");
  104.  
  105.    if (color_type == PNG_COLOR_TYPE_PALETTE &&
  106.       bit_depth == 16)
  107.       png_error(png_ptr, "Invalid color type and bit depth combination in IHDR");
  108.  
  109.    if ((color_type == PNG_COLOR_TYPE_RGB ||
  110.       color_type == PNG_COLOR_TYPE_GRAY_ALPHA ||
  111.       color_type == PNG_COLOR_TYPE_RGB_ALPHA) &&
  112.       bit_depth < 8)
  113.       png_error(png_ptr, "Invalid color type and bit depth in IHDR");
  114.  
  115.    if (interlace_type > 1)
  116.       png_error(png_ptr, "Invalid interlace method in IHDR");
  117.  
  118.    if (compression_type > 0)
  119.       png_error(png_ptr, "Invalid compression method in IHDR");
  120.  
  121.    if (filter_type > 0)
  122.       png_error(png_ptr, "Invalid filter method in IHDR");
  123.  
  124.    /* set internal variables */
  125.    png_ptr->width = width;
  126.    png_ptr->height = height;
  127.    png_ptr->bit_depth = (png_byte)bit_depth;
  128.    png_ptr->interlaced = (png_byte)interlace_type;
  129.    png_ptr->color_type = (png_byte)color_type;
  130.  
  131.    /* find number of channels */
  132.    switch (png_ptr->color_type)
  133.    {
  134.       case 0:
  135.       case 3:
  136.          png_ptr->channels = 1;
  137.          break;
  138.       case 2:
  139.          png_ptr->channels = 3;
  140.          break;
  141.       case 4:
  142.          png_ptr->channels = 2;
  143.          break;
  144.       case 6:
  145.          png_ptr->channels = 4;
  146.          break;
  147.    }
  148.    /* set up other useful info */
  149.    png_ptr->pixel_depth = (png_byte)(png_ptr->bit_depth *
  150.       png_ptr->channels);
  151.    png_ptr->rowbytes = ((png_ptr->width *
  152.       (png_uint_32)png_ptr->pixel_depth + 7) >> 3);
  153.    /* call the IHDR callback (which should just set up info) */
  154.    png_read_IHDR(png_ptr, info, width, height, bit_depth,
  155.       color_type, compression_type, filter_type, interlace_type);
  156.  
  157.    png_ptr->mode |= PNG_HAVE_IHDR;
  158. }
  159.  
  160. /* read and check the palette */
  161. void
  162. png_handle_PLTE(png_structp png_ptr, png_infop info, png_uint_32 length)
  163. {
  164.    int num, i;
  165.    png_colorp palette;
  166.  
  167.    if (!(png_ptr->mode & PNG_HAVE_IHDR))
  168.       png_error(png_ptr, "Missing IHDR before PLTE");
  169.    else if (png_ptr->mode & PNG_HAVE_PLTE)
  170.       png_error(png_ptr, "Multiple PLTE");
  171.  
  172. #if !defined(PNG_READ_OPT_PLTE_SUPPORTED)
  173.    if (png_ptr->color_type != PNG_COLOR_TYPE_PALETTE)
  174.    {
  175.       png_crc_skip(png_ptr, length);
  176.       return;
  177.    }
  178. #endif
  179.  
  180.    if (length % 3)
  181.    {
  182.       if (png_ptr->color_type != PNG_COLOR_TYPE_PALETTE)
  183.       {
  184.          png_warning(png_ptr, "Invalid palette chunk");
  185.          png_crc_skip(png_ptr, length);
  186.          return;
  187.       }
  188.       else
  189.       {
  190.          png_error(png_ptr, "Invalid palette chunk");
  191.       }
  192.    }
  193.  
  194.    num = (int)length / 3;
  195.    palette = (png_colorp)png_large_malloc(png_ptr, num * sizeof (png_color));
  196.    png_ptr->do_free |= PNG_FREE_PALETTE;
  197.    for (i = 0; i < num; i++)
  198.    {
  199.       png_byte buf[3];
  200.  
  201.       png_crc_read(png_ptr, buf, 3);
  202.       /* don't depend upon png_color being any order */
  203.       palette[i].red = buf[0];
  204.       palette[i].green = buf[1];
  205.       palette[i].blue = buf[2];
  206.    }
  207.    png_ptr->palette = palette;
  208.    png_ptr->num_palette = (png_uint_16)num;
  209.    png_read_PLTE(png_ptr, info, palette, num);
  210.  
  211.    png_ptr->mode |= PNG_HAVE_PLTE;
  212. }
  213.  
  214. #if defined(PNG_READ_gAMA_SUPPORTED)
  215. void
  216. png_handle_gAMA(png_structp png_ptr, png_infop info, png_uint_32 length)
  217. {
  218.    png_uint_32 igamma;
  219.    float gamma;
  220.    png_byte buf[4];
  221.  
  222.    if (!(png_ptr->mode & PNG_HAVE_IHDR))
  223.       png_error(png_ptr, "Missing IHDR before gAMA");
  224.    else if (png_ptr->mode & PNG_HAVE_PLTE)
  225.       /* Should be an error, but we can cope with it */
  226.       png_warning(png_ptr, "Out of place gAMA chunk");
  227.  
  228.    if (length != 4)
  229.    {
  230.       png_warning(png_ptr, "Incorrect gAMA chunk length");
  231.       png_crc_skip(png_ptr, length);
  232.       return;
  233.    }
  234.  
  235.    png_crc_read(png_ptr, buf, 4);
  236.    igamma = png_get_uint_32(buf);
  237.    /* check for zero gamma */
  238.    if (!igamma)
  239.       return;
  240.  
  241.    gamma = (float)igamma / (float)100000.0;
  242.    png_read_gAMA(png_ptr, info, gamma);
  243.    png_ptr->gamma = gamma;
  244. }
  245. #endif
  246.  
  247. #if defined(PNG_READ_sBIT_SUPPORTED)
  248. void
  249. png_handle_sBIT(png_structp png_ptr, png_infop info, png_uint_32 length)
  250. {
  251.    int slen;
  252.    png_byte buf[4];
  253.  
  254.    buf[0] = buf[1] = buf[2] = buf[3] = 0;
  255.  
  256.    if (!(png_ptr->mode & PNG_HAVE_IHDR))
  257.       png_error(png_ptr, "Missing IHDR before sBIT");
  258.    else if (png_ptr->mode & PNG_HAVE_PLTE)
  259.       /* Should be an error, but we can cope with it */
  260.       png_warning(png_ptr, "Out of place sBIT chunk");
  261.  
  262.    if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
  263.       slen = 3;
  264.    else
  265.       slen = png_ptr->channels;
  266.  
  267.    if (length != (png_uint_32)slen)
  268.    {
  269.       png_warning(png_ptr, "Incorrect sBIT chunk length");
  270.       png_crc_skip(png_ptr, length);
  271.       return;
  272.    }
  273.  
  274.    png_crc_read(png_ptr, buf, length);
  275.    if (png_ptr->color_type & PNG_COLOR_MASK_COLOR)
  276.    {
  277.       png_ptr->sig_bit.red = buf[0];
  278.       png_ptr->sig_bit.green = buf[1];
  279.       png_ptr->sig_bit.blue = buf[2];
  280.       png_ptr->sig_bit.alpha = buf[3];
  281.    }
  282.    else
  283.    {
  284.       png_ptr->sig_bit.gray = buf[0];
  285.       png_ptr->sig_bit.alpha = buf[1];
  286.    }
  287.    png_read_sBIT(png_ptr, info, &(png_ptr->sig_bit));
  288. }
  289. #endif
  290.  
  291. #if defined(PNG_READ_cHRM_SUPPORTED)
  292. void
  293. png_handle_cHRM(png_structp png_ptr, png_infop info, png_uint_32 length)
  294. {
  295.    png_byte buf[4];
  296.    png_uint_32 v;
  297.    float white_x, white_y, red_x, red_y, green_x, green_y, blue_x, blue_y;
  298.  
  299.    if (!(png_ptr->mode & PNG_HAVE_IHDR))
  300.       png_error(png_ptr, "Missing IHDR before sBIT");
  301.    else if (png_ptr->mode & PNG_HAVE_PLTE)
  302.       /* Should be an error, but we can cope with it */
  303.       png_warning(png_ptr, "Missing PLTE before cHRM");
  304.  
  305.    if (length != 32)
  306.    {
  307.       png_warning(png_ptr, "Incorrect cHRM chunk length");
  308.       png_crc_skip(png_ptr, length);
  309.       return;
  310.    }
  311.  
  312.    png_crc_read(png_ptr, buf, 4);
  313.    v = png_get_uint_32(buf);
  314.    white_x = (float)v / (float)100000.0;
  315.  
  316.    png_crc_read(png_ptr, buf, 4);
  317.    v = png_get_uint_32(buf);
  318.    white_y = (float)v / (float)100000.0;
  319.  
  320.    png_crc_read(png_ptr, buf, 4);
  321.    v = png_get_uint_32(buf);
  322.    red_x = (float)v / (float)100000.0;
  323.  
  324.    png_crc_read(png_ptr, buf, 4);
  325.    v = png_get_uint_32(buf);
  326.    red_y = (float)v / (float)100000.0;
  327.  
  328.    png_crc_read(png_ptr, buf, 4);
  329.    v = png_get_uint_32(buf);
  330.    green_x = (float)v / (float)100000.0;
  331.  
  332.    png_crc_read(png_ptr, buf, 4);
  333.    v = png_get_uint_32(buf);
  334.    green_y = (float)v / (float)100000.0;
  335.  
  336.    png_crc_read(png_ptr, buf, 4);
  337.    v = png_get_uint_32(buf);
  338.    blue_x = (float)v / (float)100000.0;
  339.  
  340.    png_crc_read(png_ptr, buf, 4);
  341.    v = png_get_uint_32(buf);
  342.    blue_y = (float)v / (float)100000.0;
  343.  
  344.    png_read_cHRM(png_ptr, info,
  345.       white_x, white_y, red_x, red_y, green_x, green_y, blue_x, blue_y);
  346. }
  347. #endif
  348.  
  349. #if defined(PNG_READ_tRNS_SUPPORTED)
  350. void
  351. png_handle_tRNS(png_structp png_ptr, png_infop info, png_uint_32 length)
  352. {
  353.    if (!(png_ptr->mode & PNG_HAVE_IHDR))
  354.       png_error(png_ptr, "Missing IHDR before tRNS");
  355.  
  356.    if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
  357.    {
  358.       if (!(png_ptr->mode & PNG_HAVE_PLTE))
  359.       {
  360.          /* Should be an error, but we can cope with it */
  361.          png_warning(png_ptr, "Missing PLTE before tRNS");
  362.       }
  363.       else if (length > png_ptr->num_palette)
  364.       {
  365.          png_warning(png_ptr, "Incorrect tRNS chunk length");
  366.          png_crc_skip(png_ptr, length);
  367.          return;
  368.       }
  369.  
  370.       png_ptr->trans = (png_bytep)png_large_malloc(png_ptr, length);
  371.       png_ptr->do_free |= PNG_FREE_TRANS;
  372.       png_crc_read(png_ptr, png_ptr->trans, length);
  373.       png_ptr->num_trans = (png_uint_16)length;
  374.    }
  375.    else if (png_ptr->color_type == PNG_COLOR_TYPE_RGB)
  376.    {
  377.       png_byte buf[6];
  378.  
  379.       if (length != 6)
  380.       {
  381.          png_warning(png_ptr, "Incorrect tRNS chunk length");
  382.          png_crc_skip(png_ptr, length);
  383.          return;
  384.       }
  385.  
  386.       png_crc_read(png_ptr, buf, length);
  387.       png_ptr->num_trans = 3;
  388.       png_ptr->trans_values.red = png_get_uint_16(buf);
  389.       png_ptr->trans_values.green = png_get_uint_16(buf + 2);
  390.       png_ptr->trans_values.blue = png_get_uint_16(buf + 4);
  391.    }
  392.    else if (png_ptr->color_type == PNG_COLOR_TYPE_GRAY)
  393.    {
  394.       png_byte buf[6];
  395.  
  396.       if (length != 2)
  397.       {
  398.          png_warning(png_ptr, "Incorrect tRNS chunk length");
  399.          png_crc_skip(png_ptr, length);
  400.          return;
  401.       }
  402.  
  403.       png_crc_read(png_ptr, buf, 2);
  404.       png_ptr->num_trans = 1;
  405.       png_ptr->trans_values.gray = png_get_uint_16(buf);
  406.    }
  407.    else
  408.    {
  409.       png_warning(png_ptr, "tRNS chunk not allowed with alpha channel");
  410.       png_crc_skip(png_ptr, length);
  411.       return;
  412.    }
  413.  
  414.    png_read_tRNS(png_ptr, info, png_ptr->trans, png_ptr->num_trans,
  415.       &(png_ptr->trans_values));
  416. }
  417. #endif
  418.  
  419. #if defined(PNG_READ_bKGD_SUPPORTED)
  420. void
  421. png_handle_bKGD(png_structp png_ptr, png_infop info, png_uint_32 length)
  422. {
  423.    int truelen;
  424.    png_byte buf[6];
  425.  
  426.    if (!(png_ptr->mode & PNG_HAVE_IHDR))
  427.       png_error(png_ptr, "Missing IHDR before bKGD");
  428.    else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE &&
  429.             !(png_ptr->mode & PNG_HAVE_PLTE))
  430.    {
  431.       png_warning(png_ptr, "Missing PLTE before bKGD");
  432.       png_crc_skip(png_ptr, length);
  433.       return;
  434.    }
  435.  
  436.    if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
  437.       truelen = 1;
  438.    else if (png_ptr->color_type & PNG_COLOR_MASK_COLOR)
  439.       truelen = 6;
  440.    else
  441.       truelen = 2;
  442.  
  443.    if (length != (png_uint_32)truelen)
  444.    {
  445.       png_warning(png_ptr, "Incorrect bKGD chunk length");
  446.       png_crc_skip(png_ptr, length);
  447.       return;
  448.    }
  449.  
  450.    png_crc_read(png_ptr, buf, length);
  451.    /* We convert the index value into RGB components so that we can allow
  452.     * arbitrary RGB values for background when we have transparency, and
  453.     * so it is easy to determine the RGB values of the background color
  454.     * from the info_ptr. */
  455.    if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
  456.    {
  457.       png_ptr->background.index = buf[0];
  458.       png_ptr->background.red = (png_uint_16)png_ptr->palette[buf[0]].red;
  459.       png_ptr->background.green = (png_uint_16)png_ptr->palette[buf[0]].green;
  460.       png_ptr->background.blue = (png_uint_16)png_ptr->palette[buf[0]].blue;
  461.    }
  462.    else if (!(png_ptr->color_type & PNG_COLOR_MASK_COLOR))
  463.    {
  464.       png_ptr->background.red =
  465.       png_ptr->background.green =
  466.       png_ptr->background.blue =
  467.       png_ptr->background.gray = png_get_uint_16(buf);
  468.    }
  469.    else
  470.    {
  471.       png_ptr->background.red = png_get_uint_16(buf);
  472.       png_ptr->background.green = png_get_uint_16(buf + 2);
  473.       png_ptr->background.blue = png_get_uint_16(buf + 4);
  474.    }
  475.  
  476.    png_read_bKGD(png_ptr, info, &(png_ptr->background));
  477. }
  478. #endif
  479.  
  480. #if defined(PNG_READ_hIST_SUPPORTED)
  481. void
  482. png_handle_hIST(png_structp png_ptr, png_infop info, png_uint_32 length)
  483. {
  484.    int num, i;
  485.  
  486.    if (!(png_ptr->mode & PNG_HAVE_IHDR))
  487.       png_error(png_ptr, "Missing IHDR before hIST");
  488.    else if (!(png_ptr->mode & PNG_HAVE_PLTE))
  489.    {
  490.       png_warning(png_ptr, "Missing PLTE before hIST");
  491.       png_crc_skip(png_ptr, length);
  492.       return;
  493.    }
  494.  
  495.    if (length != 2 * png_ptr->num_palette)
  496.    {
  497.       png_warning(png_ptr, "Incorrect hIST chunk length");
  498.       png_crc_skip(png_ptr, length);
  499.       return;
  500.    }
  501.  
  502.    num = (int)length / 2;
  503.    png_ptr->hist = (png_uint_16p)png_large_malloc(png_ptr,
  504.       num * sizeof (png_uint_16));
  505.    png_ptr->do_free |= PNG_FREE_HIST;
  506.    for (i = 0; i < num; i++)
  507.    {
  508.       png_byte buf[2];
  509.  
  510.       png_crc_read(png_ptr, buf, 2);
  511.       png_ptr->hist[i] = png_get_uint_16(buf);
  512.    }
  513.    png_read_hIST(png_ptr, info, png_ptr->hist);
  514. }
  515. #endif
  516.  
  517. #if defined(PNG_READ_pHYs_SUPPORTED)
  518. void
  519. png_handle_pHYs(png_structp png_ptr, png_infop info, png_uint_32 length)
  520. {
  521.    png_byte buf[9];
  522.    png_uint_32 res_x, res_y;
  523.    int unit_type;
  524.  
  525.    if (!(png_ptr->mode & PNG_HAVE_IHDR))
  526.       png_error(png_ptr, "Missing IHDR before pHYS");
  527.  
  528.    if (length != 9)
  529.    {
  530.       png_warning(png_ptr, "Incorrect pHYs chunk length");
  531.       png_crc_skip(png_ptr, length);
  532.       return;
  533.    }
  534.  
  535.    png_crc_read(png_ptr, buf, 9);
  536.  
  537.    res_x = png_get_uint_32(buf);
  538.    res_y = png_get_uint_32(buf + 4);
  539.    unit_type = buf[8];
  540.    png_read_pHYs(png_ptr, info, res_x, res_y, unit_type);
  541. }
  542. #endif
  543.  
  544. #if defined(PNG_READ_oFFs_SUPPORTED)
  545. void
  546. png_handle_oFFs(png_structp png_ptr, png_infop info, png_uint_32 length)
  547. {
  548.    png_byte buf[9];
  549.    png_uint_32 offset_x, offset_y;
  550.    int unit_type;
  551.  
  552.    if (!(png_ptr->mode & PNG_HAVE_IHDR))
  553.       png_error(png_ptr, "Missing IHDR before oFFs");
  554.  
  555.    if (length != 9)
  556.    {
  557.       png_warning(png_ptr, "Incorrect oFFs chunk length");
  558.       png_crc_skip(png_ptr, length);
  559.       return;
  560.    }
  561.  
  562.    png_crc_read(png_ptr, buf, 9);
  563.  
  564.    offset_x = png_get_uint_32(buf);
  565.    offset_y = png_get_uint_32(buf + 4);
  566.    unit_type = buf[8];
  567.    png_read_oFFs(png_ptr, info, offset_x, offset_y, unit_type);
  568. }
  569. #endif
  570.  
  571. #if defined(PNG_READ_tIME_SUPPORTED)
  572. void
  573. png_handle_tIME(png_structp png_ptr, png_infop info, png_uint_32 length)
  574. {
  575.    png_byte buf[7];
  576.    png_time mod_time;
  577.  
  578.    if (!(png_ptr->mode & PNG_HAVE_IHDR))
  579.       png_error(png_ptr, "Missing IHDR before tIME");
  580.  
  581.    if (length != 7)
  582.    {
  583.       png_warning(png_ptr, "Incorrect tIME chunk length");
  584.       png_crc_skip(png_ptr, length);
  585.       return;
  586.    }
  587.  
  588.    png_crc_read(png_ptr, buf, 7);
  589.  
  590.    mod_time.second = buf[6];
  591.    mod_time.minute = buf[5];
  592.    mod_time.hour = buf[4];
  593.    mod_time.day = buf[3];
  594.    mod_time.month = buf[2];
  595.    mod_time.year = png_get_uint_16(buf);
  596.  
  597.    png_read_tIME(png_ptr, info, &mod_time);
  598. }
  599. #endif
  600.  
  601. #if defined(PNG_READ_tEXt_SUPPORTED)
  602. /* note: this does not correctly handle chunks that are > 64K */
  603. void
  604. png_handle_tEXt(png_structp png_ptr, png_infop info, png_uint_32 length)
  605. {
  606.    png_charp key;
  607.    png_charp text;
  608.  
  609.    if (!(png_ptr->mode & PNG_HAVE_IHDR))
  610.       png_error(png_ptr, "Missing IHDR before tEXt");
  611.  
  612.    key = (png_charp )png_large_malloc(png_ptr, length + 1);
  613.    png_crc_read(png_ptr, (png_bytep )key, length);
  614.    key[(png_size_t)length] = '\0';
  615.  
  616.    for (text = key; *text; text++)
  617.       /* empty loop to check key length */ ;
  618.  
  619.    if (text != key + (png_size_t)length)
  620.       text++;
  621.  
  622.    png_read_tEXt(png_ptr, info, key, text, length - (text - key));
  623. }
  624. #endif
  625.  
  626. #if defined(PNG_READ_zTXt_SUPPORTED)
  627. /* note: this does not correctly handle chunks that are > 64K compressed
  628.    on those systems that can't malloc more than 64KB at a time. */
  629. void
  630. png_handle_zTXt(png_structp png_ptr, png_infop info, png_uint_32 length)
  631. {
  632.    static char msg[] = "Error decoding zTXt chunk";
  633.    png_charp key;
  634.    png_charp text;
  635.    png_uint_32 text_size, key_size;
  636.  
  637.    if (!(png_ptr->mode & PNG_HAVE_IHDR))
  638.       png_error(png_ptr, "Missing IHDR before zTXt");
  639.  
  640.    key = png_large_malloc(png_ptr, length + 1);
  641.    png_crc_read(png_ptr, (png_bytep )key, length);
  642.    key[(png_size_t)length] = '\0';
  643.  
  644.    for (text = key; *text; text++)
  645.       /* empty loop */ ;
  646.  
  647.    /* zTXt can't have zero text */
  648.    if (text == key + (png_size_t)length)
  649.    {
  650.       png_warning(png_ptr, "Zero length zTXt chunk");
  651.       text_size = 0;
  652.    }
  653.    else if (*(++text)) /* check compression type byte */
  654.    {
  655.       png_warning(png_ptr, "Unknown zTXt compression type");
  656.  
  657.       /* Copy what we can of the error message into the text chunk */
  658.       text_size = length - (text - key) - 1;
  659.       text_size = sizeof(msg) > text_size ? text_size : sizeof(msg);
  660.       png_memcpy(text, msg, (png_size_t)(text_size + 1));
  661.    }
  662.    else
  663.    {
  664.       text++;
  665.  
  666.       png_ptr->zstream->next_in = (png_bytep )text;
  667.       png_ptr->zstream->avail_in = (uInt)(length - (text - key));
  668.       png_ptr->zstream->next_out = png_ptr->zbuf;
  669.       png_ptr->zstream->avail_out = (png_size_t)png_ptr->zbuf_size;
  670.  
  671.       key_size = text - key;
  672.       text_size = 0;
  673.       text = NULL;
  674.  
  675.       while (png_ptr->zstream->avail_in)
  676.       {
  677.          int ret;
  678.  
  679.          ret = inflate(png_ptr->zstream, Z_PARTIAL_FLUSH);
  680.          if (ret != Z_OK && ret != Z_STREAM_END)
  681.          {
  682.             if (png_ptr->zstream->msg)
  683.                png_warning(png_ptr, png_ptr->zstream->msg);
  684.             else
  685.                png_warning(png_ptr, "zTXt decompression error");
  686.             inflateReset(png_ptr->zstream);
  687.             png_ptr->zstream->avail_in = 0;
  688.  
  689.             if (!text)
  690.             {
  691.                text_size = key_size + sizeof(msg) + 1;
  692.                text = (png_charp)png_large_malloc(png_ptr, text_size);
  693.                png_memcpy(text, key, (png_size_t)key_size);
  694.             }
  695.  
  696.             text[text_size - 1] = '\0';
  697.  
  698.             /* Copy what we can of the error message into the text chunk */
  699.             text_size = length - (text - key) - 1;
  700.             text_size = sizeof(msg) > text_size ? text_size : sizeof(msg);
  701.             png_memcpy(text + key_size, msg, (png_size_t)(text_size + 1));
  702.             break;
  703.          }
  704.          if (!png_ptr->zstream->avail_out || ret == Z_STREAM_END)
  705.          {
  706.             if (!text)
  707.             {
  708.                text = (png_charp)png_large_malloc(png_ptr,
  709.                   png_ptr->zbuf_size - png_ptr->zstream->avail_out +
  710.                      key_size + 1);
  711.                png_memcpy(text + (png_size_t)key_size, png_ptr->zbuf,
  712.                   (png_size_t)(png_ptr->zbuf_size - png_ptr->zstream->avail_out));
  713.                png_memcpy(text, key, (png_size_t)key_size);
  714.                text_size = key_size + (png_size_t)png_ptr->zbuf_size -
  715.                   png_ptr->zstream->avail_out;
  716.                *(text + (png_size_t)text_size) = '\0';
  717.             }
  718.             else
  719.             {
  720.                png_charp tmp;
  721.  
  722.                tmp = text;
  723.                text = png_large_malloc(png_ptr, text_size +
  724.                   png_ptr->zbuf_size - png_ptr->zstream->avail_out + 1);
  725.                png_memcpy(text, tmp, (png_size_t)text_size);
  726.                png_large_free(png_ptr, tmp);
  727.                png_memcpy(text + (png_size_t)text_size, png_ptr->zbuf,
  728.                   (png_size_t)(png_ptr->zbuf_size - png_ptr->zstream->avail_out));
  729.                text_size += png_ptr->zbuf_size - png_ptr->zstream->avail_out;
  730.                *(text + (png_size_t)text_size) = '\0';
  731.             }
  732.             if (ret != Z_STREAM_END)
  733.             {
  734.                png_ptr->zstream->next_out = png_ptr->zbuf;
  735.                png_ptr->zstream->avail_out = (uInt)png_ptr->zbuf_size;
  736.             }
  737.             else
  738.             {
  739.                break;
  740.             }
  741.          }
  742.       }
  743.  
  744.       inflateReset(png_ptr->zstream);
  745.       png_ptr->zstream->avail_in = 0;
  746.  
  747.       png_large_free(png_ptr, key);
  748.       key = text;
  749.       text += (png_size_t)key_size;
  750.       text_size -= key_size;
  751.    }
  752.  
  753.    png_read_zTXt(png_ptr, info, key, text, text_size, 0);
  754. }
  755. #endif
  756.  
  757. /* Combines the row recently read in with the previous row.
  758.    This routine takes care of alpha and transparency if requested.
  759.    This routine also handles the two methods of progressive display
  760.    of interlaced images, depending on the mask value.
  761.    The mask value describes which pixels are to be combined with
  762.    the row.  The pattern always repeats every 8 pixels, so just 8
  763.    bits are needed.  A one indicates the pixels is to be combined,
  764.    a zero indicates the pixel is to be skipped.  This is in addition
  765.    to any alpha or transparency value associated with the pixel.  If
  766.    you want all pixels to be combined, pass 0xff (255) in mask.
  767. */
  768. void
  769. png_combine_row(png_structp png_ptr, png_bytep row,
  770.    int mask)
  771. {
  772.    if (mask == 0xff)
  773.    {
  774.       png_memcpy(row, png_ptr->row_buf + 1,
  775.          (png_size_t)((png_ptr->width *
  776.          png_ptr->row_info.pixel_depth + 7) >> 3));
  777.    }
  778.    else
  779.    {
  780.       switch (png_ptr->row_info.pixel_depth)
  781.       {
  782.          case 1:
  783.          {
  784.             png_bytep sp;
  785.             png_bytep dp;
  786.             int m;
  787.             int shift;
  788.             png_uint_32 i;
  789.             int value;
  790.  
  791.             sp = png_ptr->row_buf + 1;
  792.             dp = row;
  793.             shift = 7;
  794.             m = 0x80;
  795.             for (i = 0; i < png_ptr->width; i++)
  796.             {
  797.                if (m & mask)
  798.                {
  799.                   value = (*sp >> shift) & 0x1;
  800.                   *dp &= (png_byte)((0x7f7f >> (7 - shift)) & 0xff);
  801.                   *dp |= (png_byte)(value << shift);
  802.                }
  803.  
  804.                if (shift == 0)
  805.                {
  806.                   shift = 7;
  807.                   sp++;
  808.                   dp++;
  809.                }
  810.                else
  811.                   shift--;
  812.  
  813.                if (m == 1)
  814.                   m = 0x80;
  815.                else
  816.                   m >>= 1;
  817.             }
  818.             break;
  819.          }
  820.          case 2:
  821.          {
  822.             png_bytep sp;
  823.             png_bytep dp;
  824.             int m;
  825.             int shift;
  826.             png_uint_32 i;
  827.             int value;
  828.  
  829.             sp = png_ptr->row_buf + 1;
  830.             dp = row;
  831.             shift = 6;
  832.             m = 0x80;
  833.             for (i = 0; i < png_ptr->width; i++)
  834.             {
  835.                if (m & mask)
  836.                {
  837.                   value = (*sp >> shift) & 0x3;
  838.                   *dp &= (png_byte)((0x3f3f >> (6 - shift)) & 0xff);
  839.                   *dp |= (png_byte)(value << shift);
  840.                }
  841.  
  842.                if (shift == 0)
  843.                {
  844.                   shift = 6;
  845.                   sp++;
  846.                   dp++;
  847.                }
  848.                else
  849.                   shift -= 2;
  850.                if (m == 1)
  851.                   m = 0x80;
  852.                else
  853.                   m >>= 1;
  854.             }
  855.             break;
  856.          }
  857.          case 4:
  858.          {
  859.             png_bytep sp;
  860.             png_bytep dp;
  861.             int m;
  862.             int shift;
  863.             png_uint_32 i;
  864.             int value;
  865.  
  866.             sp = png_ptr->row_buf + 1;
  867.             dp = row;
  868.             shift = 4;
  869.             m = 0x80;
  870.             for (i = 0; i < png_ptr->width; i++)
  871.             {
  872.                if (m & mask)
  873.                {
  874.                   value = (*sp >> shift) & 0xf;
  875.                   *dp &= (png_byte)((0xf0f >> (4 - shift)) & 0xff);
  876.                   *dp |= (png_byte)(value << shift);
  877.                }
  878.  
  879.                if (shift == 0)
  880.                {
  881.                   shift = 4;
  882.                   sp++;
  883.                   dp++;
  884.                }
  885.                else
  886.                   shift -= 4;
  887.                if (m == 1)
  888.                   m = 0x80;
  889.                else
  890.                   m >>= 1;
  891.             }
  892.             break;
  893.          }
  894.          default:
  895.          {
  896.             png_bytep sp;
  897.             png_bytep dp;
  898.             png_uint_32 i;
  899.             int pixel_bytes, m;
  900.  
  901.             pixel_bytes = (png_ptr->row_info.pixel_depth >> 3);
  902.  
  903.             sp = png_ptr->row_buf + 1;
  904.             dp = row;
  905.             m = 0x80;
  906.             for (i = 0; i < png_ptr->width; i++)
  907.             {
  908.                if (m & mask)
  909.                {
  910.                   png_memcpy(dp, sp, pixel_bytes);
  911.                }
  912.  
  913.                sp += pixel_bytes;
  914.                dp += pixel_bytes;
  915.  
  916.                if (m == 1)
  917.                   m = 0x80;
  918.                else
  919.                   m >>= 1;
  920.             }
  921.             break;
  922.          }
  923.       }
  924.    }
  925. }
  926.  
  927. #if defined(PNG_READ_INTERLACING_SUPPORTED)
  928. void
  929. png_do_read_interlace(png_row_infop row_info, png_bytep row, int pass)
  930. {
  931.    if (row && row_info)
  932.    {
  933.       png_uint_32 final_width;
  934.  
  935.       final_width = row_info->width * png_pass_inc[pass];
  936.  
  937.       switch (row_info->pixel_depth)
  938.       {
  939.          case 1:
  940.          {
  941.             png_bytep sp, dp;
  942.             int sshift, dshift;
  943.             png_byte v;
  944.             png_uint_32 i;
  945.             int j;
  946.  
  947.             sp = row + (png_size_t)((row_info->width - 1) >> 3);
  948.             sshift = 7 - (int)((row_info->width + 7) & 7);
  949.             dp = row + (png_size_t)((final_width - 1) >> 3);
  950.             dshift = 7 - (int)((final_width + 7) & 7);
  951.             for (i = row_info->width; i; i--)
  952.             {
  953.                v = (png_byte)((*sp >> sshift) & 0x1);
  954.                for (j = 0; j < png_pass_inc[pass]; j++)
  955.                {
  956.                   *dp &= (png_byte)((0x7f7f >> (7 - dshift)) & 0xff);
  957.                   *dp |= (png_byte)(v << dshift);
  958.                   if (dshift == 7)
  959.                   {
  960.                      dshift = 0;
  961.                      dp--;
  962.                   }
  963.                   else
  964.                      dshift++;
  965.                }
  966.                if (sshift == 7)
  967.                {
  968.                   sshift = 0;
  969.                   sp--;
  970.                }
  971.                else
  972.                   sshift++;
  973.             }
  974.             break;
  975.          }
  976.          case 2:
  977.          {
  978.             png_bytep sp, dp;
  979.             int sshift, dshift;
  980.             png_byte v;
  981.             png_uint_32 i, j;
  982.  
  983.             sp = row + (png_size_t)((row_info->width - 1) >> 2);
  984.             sshift = (png_size_t)((3 - ((row_info->width + 3) & 3)) << 1);
  985.             dp = row + (png_size_t)((final_width - 1) >> 2);
  986.             dshift = (png_size_t)((3 - ((final_width + 3) & 3)) << 1);
  987.             for (i = row_info->width; i; i--)
  988.             {
  989.                v = (png_byte)((*sp >> sshift) & 0x3);
  990.                for (j = 0; j < png_pass_inc[pass]; j++)
  991.                {
  992.                   *dp &= (png_byte)((0x3f3f >> (6 - dshift)) & 0xff);
  993.                   *dp |= (png_byte)(v << dshift);
  994.                   if (dshift == 6)
  995.                   {
  996.                      dshift = 0;
  997.                      dp--;
  998.                   }
  999.                   else
  1000.                      dshift += 2;
  1001.                }
  1002.                if (sshift == 6)
  1003.                {
  1004.                   sshift = 0;
  1005.                   sp--;
  1006.                }
  1007.                else
  1008.                   sshift += 2;
  1009.             }
  1010.             break;
  1011.          }
  1012.          case 4:
  1013.          {
  1014.             png_bytep sp, dp;
  1015.             int sshift, dshift;
  1016.             png_byte v;
  1017.             png_uint_32 i;
  1018.             int j;
  1019.  
  1020.             sp = row + (png_size_t)((row_info->width - 1) >> 1);
  1021.             sshift = (png_size_t)((1 - ((row_info->width + 1) & 1)) << 2);
  1022.             dp = row + (png_size_t)((final_width - 1) >> 1);
  1023.             dshift = (png_size_t)((1 - ((final_width + 1) & 1)) << 2);
  1024.             for (i = row_info->width; i; i--)
  1025.             {
  1026.                v = (png_byte)((*sp >> sshift) & 0xf);
  1027.                for (j = 0; j < png_pass_inc[pass]; j++)
  1028.                {
  1029.                   *dp &= (png_byte)((0xf0f >> (4 - dshift)) & 0xff);
  1030.                   *dp |= (png_byte)(v << dshift);
  1031.                   if (dshift == 4)
  1032.                   {
  1033.                      dshift = 0;
  1034.                      dp--;
  1035.                   }
  1036.                   else
  1037.                      dshift = 4;
  1038.                }
  1039.                if (sshift == 4)
  1040.                {
  1041.                   sshift = 0;
  1042.                   sp--;
  1043.                }
  1044.                else
  1045.                   sshift = 4;
  1046.             }
  1047.             break;
  1048.          }
  1049.          default:
  1050.          {
  1051.             png_bytep sp, dp;
  1052.             png_byte v[8];
  1053.             png_uint_32 i;
  1054.             int j;
  1055.             int pixel_bytes;
  1056.  
  1057.             pixel_bytes = (row_info->pixel_depth >> 3);
  1058.  
  1059.             sp = row + (png_size_t)((row_info->width - 1) * pixel_bytes);
  1060.             dp = row + (png_size_t)((final_width - 1) * pixel_bytes);
  1061.             for (i = row_info->width; i; i--)
  1062.             {
  1063.                png_memcpy(v, sp, pixel_bytes);
  1064.                for (j = 0; j < png_pass_inc[pass]; j++)
  1065.                {
  1066.                   png_memcpy(dp, v, pixel_bytes);
  1067.                   dp -= pixel_bytes;
  1068.                }
  1069.                sp -= pixel_bytes;
  1070.             }
  1071.             break;
  1072.          }
  1073.       }
  1074.       row_info->width = final_width;
  1075.       row_info->rowbytes = ((final_width *
  1076.          (png_uint_32)row_info->pixel_depth + 7) >> 3);
  1077.    }
  1078. }
  1079. #endif
  1080.  
  1081. void
  1082. png_read_filter_row(png_structp png_ptr, png_row_infop row_info, png_bytep row,
  1083.    png_bytep prev_row, int filter)
  1084. {
  1085.    switch (filter)
  1086.    {
  1087.       case 0:
  1088.          break;
  1089.       case 1:
  1090.       {
  1091.          png_uint_32 i;
  1092.          int bpp;
  1093.          png_bytep rp;
  1094.          png_bytep lp;
  1095.  
  1096.          bpp = (row_info->pixel_depth + 7) / 8;
  1097.          for (i = (png_uint_32)bpp, rp = row + bpp, lp = row;
  1098.             i < row_info->rowbytes; i++, rp++, lp++)
  1099.          {
  1100.             *rp = (png_byte)(((int)(*rp) + (int)(*lp)) & 0xff);
  1101.          }
  1102.          break;
  1103.       }
  1104.       case 2:
  1105.       {
  1106.          png_uint_32 i;
  1107.          png_bytep rp;
  1108.          png_bytep pp;
  1109.  
  1110.          for (i = 0, rp = row, pp = prev_row;
  1111.             i < row_info->rowbytes; i++, rp++, pp++)
  1112.          {
  1113.             *rp = (png_byte)(((int)(*rp) + (int)(*pp)) & 0xff);
  1114.          }
  1115.          break;
  1116.       }
  1117.       case 3:
  1118.       {
  1119.          png_uint_32 i;
  1120.          int bpp;
  1121.          png_bytep rp;
  1122.          png_bytep pp;
  1123.          png_bytep lp;
  1124.  
  1125.          bpp = (row_info->pixel_depth + 7) / 8;
  1126.          for (i = 0, rp = row, pp = prev_row;
  1127.             i < (png_uint_32)bpp; i++, rp++, pp++)
  1128.          {
  1129.             *rp = (png_byte)(((int)(*rp) +
  1130.                ((int)(*pp) / 2)) & 0xff);
  1131.          }
  1132.          for (lp = row; i < row_info->rowbytes; i++, rp++, lp++, pp++)
  1133.          {
  1134.             *rp = (png_byte)(((int)(*rp) +
  1135.                (int)(*pp + *lp) / 2) & 0xff);
  1136.          }
  1137.          break;
  1138.       }
  1139.       case 4:
  1140.       {
  1141.          int bpp;
  1142.          png_uint_32 i;
  1143.          png_bytep rp;
  1144.          png_bytep pp;
  1145.          png_bytep lp;
  1146.          png_bytep cp;
  1147.  
  1148.          bpp = (row_info->pixel_depth + 7) / 8;
  1149.          for (i = 0, rp = row, pp = prev_row,
  1150.             lp = row - bpp, cp = prev_row - bpp;
  1151.             i < row_info->rowbytes; i++, rp++, pp++, lp++, cp++)
  1152.          {
  1153.             int a, b, c, pa, pb, pc, p;
  1154.  
  1155.             b = *pp;
  1156.             if (i >= (png_uint_32)bpp)
  1157.             {
  1158.                c = *cp;
  1159.                a = *lp;
  1160.             }
  1161.             else
  1162.             {
  1163.                a = c = 0;
  1164.             }
  1165.             p = a + b - c;
  1166.             pa = abs(p - a);
  1167.             pb = abs(p - b);
  1168.             pc = abs(p - c);
  1169.  
  1170.             if (pa <= pb && pa <= pc)
  1171.                p = a;
  1172.             else if (pb <= pc)
  1173.                p = b;
  1174.             else
  1175.                p = c;
  1176.  
  1177.             *rp = (png_byte)(((int)(*rp) + p) & 0xff);
  1178.          }
  1179.          break;
  1180.       }
  1181.       default:
  1182.          png_error(png_ptr, "Bad adaptive filter type");
  1183.          break;
  1184.    }
  1185. }
  1186.  
  1187. void
  1188. png_read_finish_row(png_structp png_ptr)
  1189. {
  1190.    png_ptr->row_number++;
  1191.    if (png_ptr->row_number < png_ptr->num_rows)
  1192.       return;
  1193.  
  1194.    if (png_ptr->interlaced)
  1195.    {
  1196.       png_ptr->row_number = 0;
  1197.       png_memset(png_ptr->prev_row, 0, (png_size_t)png_ptr->rowbytes + 1);
  1198.       do
  1199.       {
  1200.          png_ptr->pass++;
  1201.          if (png_ptr->pass >= 7)
  1202.             break;
  1203.          png_ptr->iwidth = (png_ptr->width +
  1204.             png_pass_inc[png_ptr->pass] - 1 -
  1205.             png_pass_start[png_ptr->pass]) /
  1206.             png_pass_inc[png_ptr->pass];
  1207.          png_ptr->irowbytes = ((png_ptr->iwidth *
  1208.             png_ptr->pixel_depth + 7) >> 3) + 1;
  1209.          if (!(png_ptr->transformations & PNG_INTERLACE))
  1210.          {
  1211.             png_ptr->num_rows = (png_ptr->height +
  1212.                png_pass_yinc[png_ptr->pass] - 1 -
  1213.                png_pass_ystart[png_ptr->pass]) /
  1214.                png_pass_yinc[png_ptr->pass];
  1215.             if (!(png_ptr->num_rows))
  1216.                continue;
  1217.          }
  1218.          if (png_ptr->transformations & PNG_INTERLACE)
  1219.             break;
  1220.       } while (png_ptr->iwidth == 0);
  1221.  
  1222.       if (png_ptr->pass < 7)
  1223.          return;
  1224.    }
  1225.  
  1226.    if (!(png_ptr->flags & PNG_FLAG_ZLIB_FINISHED))
  1227.    {
  1228.       char extra;
  1229.       int ret;
  1230.  
  1231.       png_ptr->zstream->next_out = (Byte *)&extra;
  1232.       png_ptr->zstream->avail_out = (uInt)1;
  1233.       do
  1234.       {
  1235.          if (!(png_ptr->zstream->avail_in))
  1236.          {
  1237.             while (!png_ptr->idat_size)
  1238.             {
  1239.                png_byte buf[4];
  1240.                png_uint_32 crc;
  1241.  
  1242.                png_read_data(png_ptr, buf, 4);
  1243.                crc = png_get_uint_32(buf);
  1244.                if (((crc ^ 0xffffffffL) & 0xffffffffL) !=
  1245.                   (png_ptr->crc & 0xffffffffL))
  1246.                   png_warning(png_ptr, "Bad CRC value");
  1247.  
  1248.                png_read_data(png_ptr, buf, 4);
  1249.                png_ptr->idat_size = png_get_uint_32(buf);
  1250.                png_reset_crc(png_ptr);
  1251.  
  1252.                png_crc_read(png_ptr, buf, 4);
  1253.                if (png_memcmp(buf, png_IDAT, 4))
  1254.                   png_error(png_ptr, "Not enough image data");
  1255.  
  1256.             }
  1257.             png_ptr->zstream->avail_in = (uInt)png_ptr->zbuf_size;
  1258.             png_ptr->zstream->next_in = png_ptr->zbuf;
  1259.             if (png_ptr->zbuf_size > png_ptr->idat_size)
  1260.                png_ptr->zstream->avail_in = (uInt)png_ptr->idat_size;
  1261.             png_crc_read(png_ptr, png_ptr->zbuf, png_ptr->zstream->avail_in);
  1262.             png_ptr->idat_size -= png_ptr->zstream->avail_in;
  1263.          }
  1264.          ret = inflate(png_ptr->zstream, Z_PARTIAL_FLUSH);
  1265.          if (ret == Z_STREAM_END)
  1266.          {
  1267.             if (!(png_ptr->zstream->avail_out) || png_ptr->zstream->avail_in ||
  1268.                png_ptr->idat_size)
  1269.                png_error(png_ptr, "Extra compressed data");
  1270.             png_ptr->mode |= PNG_AT_LAST_IDAT;
  1271.             break;
  1272.          }
  1273.          if (ret != Z_OK)
  1274.             png_error(png_ptr, png_ptr->zstream->msg ? png_ptr->zstream->msg :
  1275.                       "Decompression Error");
  1276.  
  1277.          if (!(png_ptr->zstream->avail_out))
  1278.             png_error(png_ptr, "Extra compressed data");
  1279.  
  1280.       } while (1);
  1281.       png_ptr->zstream->avail_out = 0;
  1282.    }
  1283.  
  1284.    if (png_ptr->idat_size || png_ptr->zstream->avail_in)
  1285.       png_error(png_ptr, "Extra compression data");
  1286.  
  1287.    inflateReset(png_ptr->zstream);
  1288.  
  1289.    png_ptr->mode |= PNG_AT_LAST_IDAT;
  1290. }
  1291.  
  1292. void
  1293. png_read_start_row(png_structp png_ptr)
  1294. {
  1295.    int max_pixel_depth;
  1296.    png_uint_32 rowbytes;
  1297.  
  1298.    png_ptr->zstream->avail_in = 0;
  1299.    png_init_read_transformations(png_ptr);
  1300.    if (png_ptr->interlaced)
  1301.    {
  1302.       if (!(png_ptr->transformations & PNG_INTERLACE))
  1303.          png_ptr->num_rows = (png_ptr->height + png_pass_yinc[0] - 1 -
  1304.             png_pass_ystart[0]) / png_pass_yinc[0];
  1305.       else
  1306.          png_ptr->num_rows = png_ptr->height;
  1307.  
  1308.       png_ptr->iwidth = (png_ptr->width +
  1309.          png_pass_inc[png_ptr->pass] - 1 -
  1310.          png_pass_start[png_ptr->pass]) /
  1311.          png_pass_inc[png_ptr->pass];
  1312.       png_ptr->irowbytes = ((png_ptr->iwidth *
  1313.          png_ptr->pixel_depth + 7) >> 3) + 1;
  1314.    }
  1315.    else
  1316.    {
  1317.       png_ptr->num_rows = png_ptr->height;
  1318.       png_ptr->iwidth = png_ptr->width;
  1319.       png_ptr->irowbytes = png_ptr->rowbytes + 1;
  1320.    }
  1321.  
  1322.    max_pixel_depth = png_ptr->pixel_depth;
  1323.  
  1324. #if defined(PNG_READ_PACK_SUPPORTED)
  1325.    if ((png_ptr->transformations & PNG_PACK) && png_ptr->bit_depth < 8)
  1326.       max_pixel_depth = 8;
  1327. #endif
  1328.  
  1329. #if defined(PNG_READ_EXPAND_SUPPORTED)
  1330.    if (png_ptr->transformations & PNG_EXPAND)
  1331.    {
  1332.       if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
  1333.       {
  1334.          if (png_ptr->num_trans)
  1335.             max_pixel_depth = 32;
  1336.          else
  1337.             max_pixel_depth = 24;
  1338.       }
  1339.       else if (png_ptr->color_type == PNG_COLOR_TYPE_GRAY)
  1340.       {
  1341.          if (max_pixel_depth < 8)
  1342.             max_pixel_depth = 8;
  1343.          if (png_ptr->num_trans)
  1344.             max_pixel_depth *= 2;
  1345.       }
  1346.       else if (png_ptr->color_type == PNG_COLOR_TYPE_RGB)
  1347.       {
  1348.          if (png_ptr->num_trans)
  1349.          {
  1350.             max_pixel_depth *= 4;
  1351.             max_pixel_depth /= 3;
  1352.          }
  1353.       }
  1354.    }
  1355. #endif
  1356.  
  1357. #if defined(PNG_READ_FILLER_SUPPORTED)
  1358.    if (png_ptr->transformations & (PNG_FILLER))
  1359.    {
  1360.       if (max_pixel_depth < 32)
  1361.          max_pixel_depth = 32;
  1362.    }
  1363. #endif
  1364.  
  1365. #if defined(PNG_READ_GRAY_TO_RGB_SUPPORTED)
  1366.    if (png_ptr->transformations & PNG_GRAY_TO_RGB)
  1367.    {
  1368.       if ((png_ptr->num_trans && (png_ptr->transformations & PNG_EXPAND)) ||
  1369.          png_ptr->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
  1370.       {
  1371.          if (max_pixel_depth <= 16)
  1372.             max_pixel_depth = 32;
  1373.          else if (max_pixel_depth <= 32)
  1374.             max_pixel_depth = 64;
  1375.       }
  1376.       else
  1377.       {
  1378.          if (max_pixel_depth <= 8)
  1379.             max_pixel_depth = 24;
  1380.          else if (max_pixel_depth <= 16)
  1381.             max_pixel_depth = 48;
  1382.       }
  1383.    }
  1384. #endif
  1385.  
  1386.    /* align the width on the next larger 8 pixels.  Mainly used
  1387.       for interlacing */
  1388.    rowbytes = ((png_ptr->width + 7) & ~((png_uint_32)7));
  1389.    /* calculate the maximum bytes needed, adding a byte and a pixel
  1390.       for safety sake */
  1391.    rowbytes = ((rowbytes * (png_uint_32)max_pixel_depth + 7) >> 3) +
  1392.       1 + ((max_pixel_depth + 7) >> 3);
  1393. #ifdef PNG_MAX_MALLOC_64K
  1394.    if (rowbytes > 65536L)
  1395.       png_error(png_ptr, "This image requires a row greater than 64KB");
  1396. #endif
  1397.    png_ptr->row_buf = (png_bytep )png_large_malloc(png_ptr, rowbytes);
  1398.  
  1399. #ifdef PNG_MAX_MALLOC_64K
  1400.    if (png_ptr->rowbytes + 1 > 65536L)
  1401.       png_error(png_ptr, "This image requires a row greater than 64KB");
  1402. #endif
  1403.    png_ptr->prev_row = png_large_malloc(png_ptr, png_ptr->rowbytes + 1);
  1404.  
  1405.    png_memset(png_ptr->prev_row, 0, (png_size_t)png_ptr->rowbytes + 1);
  1406.  
  1407.    png_ptr->flags |= PNG_FLAG_ROW_INIT;
  1408. }
  1409.  
  1410.